/*
 * Copyright (c) 2025 CAX Conversion Project
 */
package com.comac.ins.isfd.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.comac.ins.common.core.utils.MapstructUtils;
import com.comac.ins.common.mybatis.core.page.PageQuery;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.comac.ins.isfd.domain.bo.IsfdDatabaseDataImportRecordBo;
import com.comac.ins.isfd.domain.vo.IsfdDatabaseDataImportRecordVo;
import com.comac.ins.isfd.domain.IsfdDatabaseDataImportRecord;
import com.comac.ins.isfd.mapper.IsfdDatabaseDataImportRecordMapper;
import com.comac.ins.isfd.service.IIsfdDatabaseDataImportRecordService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 数据导入记录Service业务层处理
 *
 * @author Lion Li
 * @date 2024-12-06
 */
@RequiredArgsConstructor
@Service
public class IsfdDatabaseDataImportRecordServiceImpl implements IIsfdDatabaseDataImportRecordService {

    private final IsfdDatabaseDataImportRecordMapper baseMapper;

    /**
     * 查询数据导入记录
     *
     * @param id 主键
     * @return 数据导入记录
     */
    @Override
    public IsfdDatabaseDataImportRecordVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询数据导入记录列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 数据导入记录分页列表
     */
    @Override
    public TableDataInfo<IsfdDatabaseDataImportRecordVo> queryPageList(IsfdDatabaseDataImportRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IsfdDatabaseDataImportRecord> lqw = buildQueryWrapper(bo);
        Page<IsfdDatabaseDataImportRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的数据导入记录列表
     *
     * @param bo 查询条件
     * @return 数据导入记录列表
     */
    @Override
    public List<IsfdDatabaseDataImportRecordVo> queryList(IsfdDatabaseDataImportRecordBo bo) {
        LambdaQueryWrapper<IsfdDatabaseDataImportRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IsfdDatabaseDataImportRecord> buildQueryWrapper(IsfdDatabaseDataImportRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IsfdDatabaseDataImportRecord> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getModelName()), IsfdDatabaseDataImportRecord::getModelName, bo.getModelName());
        lqw.eq(StringUtils.isNotBlank(bo.getBusinessScenario()), IsfdDatabaseDataImportRecord::getBusinessScenario, bo.getBusinessScenario());
        lqw.eq(StringUtils.isNotBlank(bo.getBusinessSystem()), IsfdDatabaseDataImportRecord::getBusinessSystem, bo.getBusinessSystem());
        lqw.eq(StringUtils.isNotBlank(bo.getDatabaseType()), IsfdDatabaseDataImportRecord::getDatabaseType, bo.getDatabaseType());
        lqw.eq(StringUtils.isNotBlank(bo.getDatabaseSubtype()), IsfdDatabaseDataImportRecord::getDatabaseSubtype, bo.getDatabaseSubtype());
        lqw.like(StringUtils.isNotBlank(bo.getTableName()), IsfdDatabaseDataImportRecord::getTableName, bo.getTableName());
        lqw.eq(bo.getDataQualityId() != null, IsfdDatabaseDataImportRecord::getDataQualityId, bo.getDataQualityId());
        lqw.eq(bo.getDataConversionId() != null, IsfdDatabaseDataImportRecord::getDataConversionId, bo.getDataConversionId());
        lqw.eq(bo.getDataTemplateId() != null, IsfdDatabaseDataImportRecord::getDataTemplateId, bo.getDataTemplateId());
        lqw.like(StringUtils.isNotBlank(bo.getDataTemplateName()), IsfdDatabaseDataImportRecord::getDataTemplateName, bo.getDataTemplateName());
        lqw.eq(bo.getDataQuantity() != null, IsfdDatabaseDataImportRecord::getDataQuantity, bo.getDataQuantity());
        lqw.eq(StringUtils.isNotBlank(bo.getImportMethod()), IsfdDatabaseDataImportRecord::getImportMethod, bo.getImportMethod());
        lqw.eq(StringUtils.isNotBlank(bo.getDataForm()), IsfdDatabaseDataImportRecord::getDataForm, bo.getDataForm());
        lqw.eq(StringUtils.isNotBlank(bo.getDataFileUrl()), IsfdDatabaseDataImportRecord::getDataFileUrl, bo.getDataFileUrl());
        lqw.eq(bo.getDataImportTime() != null, IsfdDatabaseDataImportRecord::getDataImportTime, bo.getDataImportTime());
        lqw.eq(StringUtils.isNotBlank(bo.getDataImportBy()), IsfdDatabaseDataImportRecord::getDataImportBy, bo.getDataImportBy());
        lqw.eq(StringUtils.isNotBlank(bo.getDataSource()), IsfdDatabaseDataImportRecord::getDataSource, bo.getDataSource());
        lqw.eq(StringUtils.isNotBlank(bo.getComment()), IsfdDatabaseDataImportRecord::getComment, bo.getComment());
        lqw.eq(bo.getIsDelete() != null, IsfdDatabaseDataImportRecord::getIsDelete, bo.getIsDelete());
        return lqw;
    }

    /**
     * 新增数据导入记录
     *
     * @param bo 数据导入记录
     * @return 是否新增成功
     */
    @Override
    @Transactional
    public Boolean insertByBo(IsfdDatabaseDataImportRecordBo bo) {
        IsfdDatabaseDataImportRecord add = MapstructUtils.convert(bo, IsfdDatabaseDataImportRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改数据导入记录
     *
     * @param bo 数据导入记录
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(IsfdDatabaseDataImportRecordBo bo) {
        IsfdDatabaseDataImportRecord update = MapstructUtils.convert(bo, IsfdDatabaseDataImportRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IsfdDatabaseDataImportRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除数据导入记录信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
